<template>
	<view class="">
		<!-- commontext -->
		<view class="main_title">
			项目信息
		</view>
		<view class="form_box">
			<view class="input_block flex justify align">
				<view class="input_title">
					队伍类型
				</view>
				<view class="input_text flex align flex_right">
					<picker @change="bindPickerType" :value="typeInd" range-key="label" :range="typeArr">
						<view class="uni-input">{{typeArr[typeInd].label}}</view>
						<view class="uni-input" v-if="typeInd == -1">请选择队伍类型</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					项目类型
				</view>
				<view class="input_text flex align flex_right" @tap="toList">
					<text v-if="workInd == ''">请选择项目类型</text>
					<text v-else>{{workName}}</text>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					工程量
				</view>
				<view class="input_text">
					<input type="text" v-model="form.quantities" placeholder="填写工程量"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					工期
				</view>
				<view class="input_text">
					<input type="text" v-model="form.time_limit" placeholder="填写工期"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					项目所处阶段
				</view>
				<view class="input_text flex align flex_right">
					<picker @change="bindPickerstage" :value="stageInd" :range="stageArr">
						<view class="uni-input">{{form.projec_stage}}</view>
						<view class="gray" v-if="!form.projec_stage">
							选择项目所处阶段
						</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					资源来源
				</view>
				<view class="input_text">
					<input type="text" v-model="form.source_resources" placeholder="填写资源来源"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					预计进场时间
				</view>
				<view class="input_text">
					<input type="text" v-model="form.enter_time" placeholder="填写预计进场时间"/>
				</view>
			</view>
			<!-- <view class="input_block flex justify align">
				<view class="input_title">
					工价
				</view>
				<view class="input_text">
					<input type="text" v-model="form.price" placeholder="填写工价"/>
				</view>
			</view> -->
			<!-- <view class="input_block flex justify align">
				<view class="input_title">
					结算比例(月结)
				</view>
				<view class="input_text">
					<input type="text" v-model="form.settlement_prop" placeholder="填写结算比例(月结)"/>
				</view>
			</view> -->
			<view class="input_block flex justify align">
				<view class="input_title">
					项目地点
				</view>
				<view class="input_text flex align flex_right">
					
					<picker mode="region"  @change="pickerAddr">
						<view>{{form.province}} {{form.city}} {{form.area}}</view>
						<view class="" v-if="!form.province">
							请选择地区
						</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="main_title">
			发包信息
		</view>
		<view class="form_box">
			<view class="input_block flex justify align">
				<view class="input_title">
					发包人角色
				</view>
				<view class="input_text flex align flex_right">
					<picker @change="bindPickerrole" :value="roleInd" :range="roleArr">
						<view class="uni-input">{{form.employer_role}}</view>
						<view class="gray" v-if="!form.employer_role">
							选择发包人角色
						</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					发包类型
				</view>
				<view class="input_text flex align flex_right">
					<picker @change="bindPickerwrapType" :value="wrapTypeInd" :range="wrapTypeArr">
						<view class="uni-input">{{form.contract_type}}</view>
						<view class="gray" v-if="!form.contract_type">
							选择发包类型
						</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					建设单位企业性质
				</view>
				<view class="input_text">
					<input type="text" v-model="form.fb_employer_unit" placeholder="填写建设单位企业性质"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					总包单位企业性质
				</view>
				<view class="input_text">
					<input type="text" v-model="form.fb_total_employer_unit" placeholder="填写总包单位企业性质"/>
				</view>
			</view>
		</view>
		<view class="main_title">
			工程报酬
		</view>
		<view class="form_box">
			<view class="input_block flex justify align">
				<view class="input_title">
					合同价格形成
				</view>
				<view class="input_text flex align flex_right">
					<picker @change="bindPickeragreement" :value="agreementInd" :range="agreementArr">
						<view class="uni-input">{{form.price_contract}}</view>
						<view class="gray" v-if="!form.price_contract">
							选择合同价格形成
						</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					付款节点及付款比例
				</view>
				<view class="input_text">
					<input type="text" v-model="form.payment_prop" placeholder="填写付款节点及付款比例"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					结算方式
				</view>
				<view class="input_text">
					<input type="text" v-model="form.settlement_method" placeholder="填写结算方式"/>
				</view>
			</view>
		</view>
		<view class="main_title">
			项目要求
		</view>
		<view class="form_box">
			<view class="input_block flex justify align">
				<view class="input_title">
					质量要求
				</view>
				<view class="input_text">
					<input type="text" v-model="form.quality_requirement" placeholder="填写质量要求"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					业绩要求
				</view>
				<view class="input_text">
					<input type="text" v-model="form.achievement_requirement" placeholder="填写业绩要求"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					人员要求
				</view>
				<view class="input_text">
					<input type="text" v-model="form.personnel_requirement" placeholder="填写人员要求"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					资质要求
				</view>
				<view class="input_text">
					<input type="text" v-model="form.natural_requirement" placeholder="填写资质要求"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					履约保证金
				</view>
				<view class="input_text">
					<input type="text" v-model="form.performance_security" placeholder="填写缴纳比例,缴纳节点及缴纳方式"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					投标保证金
				</view>
				<view class="input_text">
					<input type="text" v-model="form.bid_security" placeholder="填写缴纳比例,缴纳节点及缴纳方式"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					保证金
				</view>
				<view class="input_text">
					<input type="text" v-model="form.security" placeholder="填写留存比例,退还时间"/>
				</view>
			</view>
			<!-- <view class="input_block flex justify align">
				<view class="input_title">
					建设单位企业性质
				</view>
				<view class="input_text flex align flex_right">
					<picker @change="bindPickerenterprise" :value="enterpriseInd" :range="enterpriseArr">
						<view class="uni-input">{{form.p_employer_unit}}</view>
						<view class="gray" v-if="!form.p_employer_unit">
							选择建设单位企业性质
						</view>
					</picker>
					<image class="right" src="../../static/icon/right2.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					总包单位企业性质
				</view>
				<view class="input_text">
					<input type="text" v-model="form.p_total_employer_unit" placeholder="填写总包单位企业性质"/>
				</view>
			</view> -->
			<view class="input_block flex justify align">
				<view class="input_title">
					项目资金到位情况
				</view>
				<view class="input_text">
					<input type="text" v-model="form.project_fund_availability" placeholder="如:地方财政已拨付预付款等"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					其他要求
				</view>
				<view class="input_text">
					<input type="text" v-model="form.other_requirement" placeholder="填写其他要求"/>
				</view>
			</view>
		</view>
		<view class="main_title">
			联系方式
		</view>
		<view class="form_box">
			<view class="input_block flex justify align">
				<view class="input_title">
					姓名
				</view>
				<view class="input_text">
					<input type="text" v-model="form.name" placeholder="填写姓名"/>
				</view>
			</view>
			<view class="input_block flex justify align">
				<view class="input_title">
					联系电话
				</view>
				<view class="input_text">
					<input type="text" v-model="form.phone" placeholder="填写联系电话"/>
				</view>
			</view>
		</view>
		<view class="main_title">
			项目介绍
		</view>
		<view class="form_box">
			<textarea name="" v-model="form.introduce" id="" cols="30" rows="10" placeholder="填入项目介绍"></textarea>
		</view>
		
		<view class="btn" @tap="sure">
			{{isEdit?'提交修改':'立即发布'}}
		</view>
	</view>

</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				isEdit:false,
				id:null,
				typeArr:[
					{
							label: '劳务班组',
							value: 200
						},
						{
							label: '专业分包',
							value: 201
						},
						{
							label: '技术团队',
							value: 202
						}
				],
				typeInd: -1,
				workInd: -1,
				stageArr:['立项已批复','项目公司已组建','初步设计投标完成','施工总包投标完成','专业分包投标完成','已取施工许可证','已取规划许可证','监理已发开工令','其他'],//项目所处阶段
				stageInd:-1,
				roleArr:['建设单位','总包','专业分包','大劳务'],//发包人角色
				roleInd:-1,
				wrapTypeArr:['总包','专业分包','大劳务','纯劳务'],//发包类型
				wrapTypeInd:-1,
				agreementArr:['单价合同','总价合同','成本+酬金'],//合同价格形式
				agreementInd:-1,
				enterpriseArr:['国企','平台','民企','央企'],//建设单位企业性质
				enterpriseInd: -1,
				workSelect:[],
				workName:'',
				workInd: '',
				form:{
					introduce:'',//	项目介绍
					troops_ids:'',//队伍类型，多个ID已英文字符逗号分割
					work_ids:'',//专业/工种，多个ID已英文字符逗号分割
					quantities:'',//	工程量
					time_limit	:'',//工期
					province:'',//省
					city:'',//市
					area:'',//区
					// price:'',//	工价
					// settlement_prop:'',//	结算比例(月结)
					name:'',//	姓名
					phone:'',//	电话
					
					projec_stage:'',//项目所处阶段
					source_resources:'',//资源来源
					enter_time:'',//预计进场时间
					employer_role:'',//发包人角色
					contract_type:'',//发包类型
					fb_employer_unit:'',//发包-建设单位企业性质
					fb_total_employer_unit:'',//发包-总包单位企业性质
					price_contract:'',//合同价格形成
					payment_prop:'',//付款节点及付款比例
					settlement_method:'',//结算方式
					quality_requirement	:'',//质量要求
					achievement_requirement	:'',//业绩要求
					personnel_requirement:'',//人员要求
					natural_requirement	:'',//资质要求
					performance_security:'',//履约保证金
					bid_security:'',//投标保证金
					security:'',//保证金
					// p_employer_unit:'',//项目要求-建设单位企业性质
					// p_total_employer_unit:'',//项目要求-总包单位企业性质
					project_fund_availability:'',//项目资金到位情况
					other_requirement:'',//其他要求
				},
			}
		},
		onLoad(options) {
			console.log('load')
			if(options.id){
				this.isEdit = true
				this.id = options.id
				
				this.getInfo()
			}
			uni.removeStorage({
				key:'work_name'
			})
		},
		onReady() {
			console.log('ready')
			if(this.isEdit){
				uni.setNavigationBarTitle({
					title:'修改项目'
				})
			}
			
		},
		onShow(){
			const _this = this
			uni.getStorage({
				key: 'work_name',
				success: function(res) {
					console.log(res.data, '缓存1--------------------');
					_this.workSelect = res.data
					let ids = []
					let  workName= []
					res.data.forEach(item=>{
						console.log(item)
						ids.push(item.uId)
						if(workName.length <3) workName.push(item.two)
					})
					_this.workName = workName.join(',')
					_this.workInd = ids.join(',')
				}
			});
		},
		methods: {
			// 编辑获取信息
			getInfo(){
				uni.request({
					url: app.globalData.IP + 'addons/shopro/project/detail',
					data: {
						id:this.id
					},
					header: {
						token: app.globalData.token
					},
					method: 'GET',
					success: res => {
						console.log('修改项目',res)
						if(res.data.code == 1){
							let data = res.data.data
							this.form.introduce = data.introduce//	项目介绍
							this.form.troops_ids = data.troops_ids//队伍类型，多个ID已英文字符逗号分割
							this.typeInd = data.troops_ids == 200 ? 0 :data.troops_ids == 201?1:2
							this.form.work_ids = data.work_ids//专业/工种，多个ID已英文字符逗号分割
							this.workName = data.work_arr.length >=3? data.work_arr[0] + ','+data.work_arr[1]+','+data.work_arr[3]:data.work_arr.join(',')
							this.workInd = data.work_ids
							this.form.quantities = data.quantities//	工程量
							this.form.time_limit	 = data.time_limit//工期
							this.form.province = data.province//省
							this.form.city = data.city//市
							this.form.area = data.area//区
							// this.form.price = data.price//	工价
							// this.form.settlement_prop = data.settlement_prop//	结算比例(月结)
							this.form.name = data.name//	姓名
							this.form.phone = data.phone//	电话
							this.form.projec_stage = data.projec_stage//项目所处阶段
							
							this.form.source_resources = data.source_resources//资源来源
							this.form.enter_time = data.enter_time//预计进场时间
							this.form.employer_role = data.employer_role//发包人角色
							this.form.contract_type = data.contract_type//发包类型
							this.form.fb_employer_unit = data.fb_employer_unit//发包-建设单位企业性质
							this.form.fb_total_employer_unit = data.fb_total_employer_unit//发包-总包单位企业性质
							this.form.price_contract = data.price_contract//合同价格形成
							this.form.payment_prop = data.payment_prop//付款节点及付款比例
							this.form.settlement_method = data.settlement_method//结算方式
							this.form.quality_requirement= data.quality_requirement//质量要求
							this.form.achievement_requirement	 = data.achievement_requirement//业绩要求
							this.form.personnel_requirement = data.personnel_requirement//人员要求
							this.form.natural_requirement	 = data.natural_requirement//资质要求
							this.form.performance_security = data.performance_security//履约保证金
							this.form.bid_security = data.bid_security//投标保证金
							this.form.security = data.security//保证金
							// this.form.p_employer_unit = data.p_employer_unit//项目要求-建设单位企业性质
							// this.form.p_total_employer_unit = data.p_total_employer_unit//项目要求-总包单位企业性质
							this.form.project_fund_availability = data.project_fund_availability//项目资金到位情况
							this.form.other_requirement = data.other_requirement//其他要求
						}else{
							uni.showToast({
								title: res.data.msg,
								icon:'none'
							})
							setTimeout(function(){
								uni.navigateBack({delta:1})
							},1000)
						}
					}
				})
			},
			toList(){
				uni.navigateTo({
					url: '/pages/typeSelection/typeSelection?pageurl=project&work='+JSON.stringify(this.workSelect)
				})
			},
			pickerAddr(e){
				console.log(e)
				this.form.province =e.detail.value[0]
				this.form.city =e.detail.value[1]
				this.form.area =e.detail.value[2]
			},
			// 
			bindPickerType(e){
				console.log(e)
				this.typeInd = e.detail.value
				this.troops_ids = this.typeArr[this.typeInd].value
			},
			// 项目所处阶段
			bindPickerstage(e){
				console.log(e)
				this.stageInd = e.detail.value
				this.form.projec_stage = this.stageArr[e.detail.value]
			},
			// 发包人角色
			bindPickerrole(e){
				console.log(e)
				this.roleInd = e.detail.value
				this.form.employer_role = this.roleArr[e.detail.value]
			},
			// 发包类型
			bindPickerwrapType(e){
				console.log(e)
				this.wrapTypeInd = e.detail.value
				this.form.contract_type = this.wrapTypeArr[e.detail.value]
			},
			// 合同价格形式
			bindPickeragreement(e){
				console.log(e)
				this.agreementInd = e.detail.value
				this.form.price_contract = this.agreementArr[e.detail.value]
			},
			// 建设单位企业性质
			bindPickerenterprise(e){
				console.log(e)
				this.enterpriseInd = e.detail.value
				this.form.p_employer_unit = this.enterpriseArr[e.detail.value]
			},
			
			// 发布
			sure(){
				if(this.typeInd <= -1 ) {
					uni.showToast({
						title: '请选择队伍类型',
						icon: 'none'
					})
					return
				}
				if(!this.workInd) {
					uni.showToast({
						title: '请选择专业/工种',
						icon: 'none'
					})
					return
				}
				if (!this.form.quantities) {
					uni.showToast({
						title: '请填入工程量',
						icon: 'none'
					})
					return
				}
				if (!this.form.province || !this.form.city || !this.form.area) {
					uni.showToast({
						title: '请选择地址',
						icon: 'none'
					})
					return
				}
				
				if (!this.form.name) {
					uni.showToast({
						title: '请填入姓名',
						icon: 'none'
					})
					return
				}
				if (!this.form.phone) {
					uni.showToast({
						title: '请填入电话',
						icon: 'none'
					})
					return
				}
				if (!this.form.introduce) {
					uni.showToast({
						title: '请填入项目介绍',
						icon: 'none'
					})
					return
				}
				let params = this.form
				params.troops_ids = this.typeArr[this.typeInd].value
				params.work_ids = this.workInd
				if(this.isEdit){
					this.form.id = this.id
					uni.request({
						url: app.globalData.IP + 'addons/shopro/project/edit',
						data: this.form,
						header: {
							token: app.globalData.token
						},
						method: 'POST',
						success: res => {
							console.log('发布项目',res)
							if(res.data.code == 1){
								uni.showToast({
									title: '修改成功'
								})
								setTimeout(function(){
									uni.navigateBack({delta:1})
								},1000)
							}else{
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								})
							}
						},
						fail: err => {
							reject(err)
							console.log('发送数据失败')
						}
					})
				}else{
					uni.request({
						url: app.globalData.IP + 'addons/shopro/project/add',
						data: params,
						header: {
							token: app.globalData.token
						},
						method: 'POST',
						success: res => {
							console.log('发布项目',res)
							if(res.data.code == 1){
								uni.showToast({
									title: '发布成功'
								})
								setTimeout(function(){
									uni.switchTab({
										url: '/pages/home/home'
									})
								},1000)
							}else{
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								})
							}
						},
						fail: err => {
							reject(err)
							console.log('发送数据失败')
						}
					})
				}
				
				
			}
		}
	}
</script>

<style>
	page{
		background: #F7F7F7;
		padding: 30rpx 0;
	}
	.form_box{
		margin: 40rpx 28rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx 28rpx;
	}
	.input_block{
		margin-bottom: 30rpx;
	}
	.input_block:last-child{
		margin-bottom: 0;
	}
	.input_block .right{
		width: 24rpx;
		height: 24rpx;
	}
	.flex_right{
		justify-content: flex-end;
	}
	.input_title{
		color: #000;
		
	}
	.input_text{
		width: 78%;
		text-align: right;
	}
	.input_text .gray{
		
	}
	.input_text picker{
		width: 78%;
	}
	.main_title{
		font-size: 30rpx;
		color: #000;
		padding: 0 28rpx;
		font-weight: 600;
	}
	.btn{
		height: 110rpx;
		line-height: 110rpx;
		text-align: center;
		color: #fff;
		background: #647DF9;
		border-radius: 100rpx;
		margin: 60rpx 28rpx 0;
	}
</style>
